<?php
namespace ZfcAdmin\Controller;
use Zend\Form\Form;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\Authentication\AuthenticationService;
use SanAuthWithDbSaveHandler\Storage\IdentityManagerInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\View\Model\JsonModel;
use Zend\Cache\StorageFactory;
class AdminController extends AbstractActionController
{

	public function indexAction()
	{
		
	}
	public function headerAction($params)
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basePath = $baseUrlArr['basePath'];
		return $this->layout()->setVariable(
			"headerarray",array(
				'baseUrl' => $baseUrl,
				'basePath' => $basePath,
			)
		);
	}
	public function getAdminLocationsAction(){
		if(isset($_POST['dist_id']) && $_POST['dist_id']){
			if(isset($_POST['location_id']) && $_POST['location_id']!=""){
				$ulocationId=$_POST['location_id'];
			}else{
				$ulocationId='';
			}			
			$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
			$locationLists = $locationTable->getAreaBasedLoations($_POST);
			$html="";
			$html.='<option value="">Select a location</option>';	
			foreach($locationLists as $location){				
				if($ulocationId==$location->location_id){
					$html.='<option value="'.$location->location_id.'" selected="selected">'.$location->location_name.'</option>';
				}
				else{
					$html.='<option value="'.$location->location_id.'">'.$location->location_name.'</option>';
				}			
			}			
		}else{
			$html="";
			$html.='<option value="">Select a location</option>';		
		}
		$result = new JsonModel(array(					
			'output' 		=> 'success',
			'loca_list'	=>	$html,
		));	
		return $result;
	}
	public function getAdminAreasAction(){
		if(isset($_POST['dist_id']) && $_POST['dist_id']!=""){
			$dist_id=$_POST['dist_id'];
			if(isset($_POST['areas_id']) && $_POST['areas_id']!=""){
				$uarea_id=$_POST['areas_id'];
			}else{
				$uarea_id='';
			}						
			$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
			$getAreas=$areaTable->getAreas($dist_id);
			$html="";
			$html.='<option value="">Select a area</option>';	
			foreach($getAreas as $area){
				if($uarea_id==$area->area_id){
					$html.='<option value="'.$area->area_id.'" selected="selected">'.$area->area_name.'</option>';					
				}else{
					$html.='<option value="'.$area->area_id.'">'.$area->area_name.'</option>';
				}		
			}			
		}else{
			$html="";
			$html.='<option value="">Select a area</option>';		
		}
		$result = new JsonModel(array(					
			'output' 		=> 'success',
			'list_areas'	=>	$html,
		));	
		return $result;	
	}
	public function adminLoginAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_POST['inputEmail']) && $_POST['inputEmail']!=""){
			$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
			if($_POST['hid']=='reg_user'){
				$regAuth='user';
			}else{
				$regAuth='admin';
			}
			$request = $this->getRequest();
			$admindetails = $usersTable->checkadminLogin($request->getPost(),$regAuth);
			if($admindetails!=''){
				if($_POST['hid']=='reg_user'){
					$user_session = new Container('user');	
				}else{
					$user_session = new Container('admin');	
				}						
				$user_session->username=$admindetails->user_name;
				$user_session->email=$admindetails->email;
				$user_session->display_name=$admindetails->user_name;
				$user_session->user_id=$admindetails->user_id;
				$user_session->user_type=$admindetails->user_type;			
				$user_session->user_type_id=$admindetails->user_type_id;			
				$result = new JsonModel(array(					
					'output' => 'success',
					'user_id' => json_decode($admindetails->user_id),
				));		
			}else{
				$result = new JsonModel(array(					
					'output' => 'not success',
				));
			}
			return $result;
		}		
	}
	public function logoutAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		unset($_SESSION['admin']);
		return $this->redirect()->toUrl($baseUrl.'admin');
	}
	public function changepasswordAction()
	{
		if(isset($_POST['cnfpwrd'])){
			$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');	
			$changepwd = $usersTable->changepwd($_POST['cnfpwrd'],$_POST['userId']);	
			if($changepwd>0){			
				$result = new JsonModel(array(					
					'output' => 'success',
				));			
			}else{
				$result = new JsonModel(array(					
					'output' => 'not success',
				));
			}
			return $result;	
		}		
	}
	public function checkPasswordAction(){ 
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		$pwdExit = $usersTable->getpassword($_POST['oldpasswrd'],$_POST['userId']);		
		if($pwdExit>0){			
			$result = new JsonModel(array(					
				'output' => 'success',
			));			
		}else{
			$result = new JsonModel(array(					
				'output' => 'not success',
			));
		}
		return $result;		
	}	
	public function addAdsTypeAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$adstypeTable=$this->getServiceLocator()->get('Category\Model\AddTypeFactory');	
		if(isset($_POST['hid_ads']) && $_POST['hid_ads']!=""){
			$add_success = $adstypeTable->addAddType($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/period-packages');
			}else{
				return $this->redirect()->toUrl($baseUrl.'admin/add-types');
			}
		}elseif(isset($_POST['ads_type_name']) && $_POST['ads_type_name']!=''){					
			$add_success = $adstypeTable->addAddType($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/add-types');
			}else{
				return new ViewModel(array(
					'output' 	=> 'not success',
				));
			}
        }else if(isset($_GET['ads_type_id']) && $_GET['ads_type_id']!=''){
			$edit_details = $adstypeTable->getadstype($_GET['ads_type_id']);
			if(count($edit_details)>0){
				return new ViewModel(array(
					'ads_details' 	=> $edit_details,
				));
			}
		}
	}
	public function totaladstypeAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';			
		$aColumns = array('ads_type_id','ads_type_name','status','ads_type_id');
		$adstypeTable=$this->getServiceLocator()->get('Category\Model\AddTypeFactory');	
		$total = $adstypeTable->getcount($_POST);
		$paginator = $adstypeTable->getadstypesGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->ads_type_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->ads_type_name;
					}
					if($result['rResult'][$m]->status != 0){
						if($i=='3'){
							$output_item ='<a href='.$baseUrl.'admin/add-ads-type?ads_type_id='.$result['rResult'][$m]->ads_type_id.'  title="Edit" class="">Edit</a>';
						}	
					}else{
						if($i=='3'){
							$output_item ='Edit';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function addTypesAction(){
	
	}
	public function addCategoryAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
		if(isset($_POST['hid_cat']) && $_POST['hid_cat']!=""){ 
			$add_success = $categoryTable->addCategory($_POST);
			if($add_success>=0){
				return $this->redirect()->toUrl($baseUrl.'admin/category');
			}else{
				return new ViewModel(array(
					'output' 	=> 'not success',
				));
			}
		}elseif(isset($_POST['cat_name']) && $_POST['cat_name']!=''){					
			$add_success = $categoryTable->addCategory($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/category');
			}else{
				return new ViewModel(array(
					'output' 	=> 'not success',
				));
			}
        }else if(isset($_GET['category_id']) && $_GET['category_id']!=''){
			$edit_details = $categoryTable->getallCategory($_GET['category_id']);
			if(count($edit_details)>0){
				return new ViewModel(array(
					'cat_details' 	=> $edit_details,
				));
			}
		}		
	}
	public function subcategoryAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$add_sub_status='';
		if(isset($_POST['category']) && $_POST['category']!=""){ 		
			$subcategoryTable=$this->getServiceLocator()->get('Category\Model\SubCategoryFactory');
			$delete_sub_categories = $subcategoryTable->deleteCategory($_POST['category']);
			foreach($_POST['name'] as $key=>$sub_cat_name){
				$add_sub_status = $subcategoryTable->addSubCategory($sub_cat_name,$_POST['category']);				
			}
			if($add_sub_status>0){
				return $this->redirect()->toUrl($baseUrl.'admin/category');
			}
		}elseif(isset($_GET['category_id']) && $_GET['category_id']!=""){	
			$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
			$subcategoryTable=$this->getServiceLocator()->get('Category\Model\SubCategoryFactory');
			$CategoryList = $categoryTable->getallCategory($_GET['category_id']);
			$sub_categories = $subcategoryTable->subCategories($_GET['category_id']);
			$count_sub_categories = $subcategoryTable->checkCategory($_GET['category_id']);
			if($count_sub_categories>0){
				$listSubcategories = $sub_categories;
			}else{
				$listSubcategories = '';
			}		
			return new ViewModel(array(
				'CategoryList' 	        => $CategoryList,
				'subcategoriesList' 	=> $listSubcategories,
				'count_sub_categories' 	=> $count_sub_categories,
			));	
		}
	}	
	public function categoryAction(){
	
	}
	public function districtsAction(){
	
	}
	public function areasAction(){
	
	}	
	public function numberWordsAction(){
	
	}
	public function periodPackagesAction(){
	
	}
	public function packagesAction(){
	
	}
	public function offersAction(){
	
	}
	public function addsClassifiedsAction(){
	
	}
	public function prepareprintPaperAction(){
	
	}
	public function ordersAction(){
	
	}			
	public function subscribeAction(){
	
	}
	public function reportsAction(){
		
	}
	public function editTestimonyAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_POST['hid_testimony']) && $_POST['hid_testimony']!=""){
			$testimonyTable=$this->getServiceLocator()->get('Users\Model\TestimonialsFactory');
			$testimony_update_status = $testimonyTable->addTestimony($_POST);
			if($testimony_update_status>=0){
				return $this->redirect()->toUrl($baseUrl.'admin/testimonial-lists');			
			}
		}else if(isset($_GET['testimony_id']) && $_GET['testimony_id']!=""){
			$testimonyTable=$this->getServiceLocator()->get('Users\Model\TestimonialsFactory');
			$testimony_details = $testimonyTable->editTestimony($_GET['testimony_id']);
			return new ViewModel(array(
				'textimony_details' 	=> $testimony_details,
			));
		}
	}
	public function testimonialListsAction(){
		
	}
	public function totaltestimonialsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';			
		$aColumns = array('review_id','user_id','review_text','status','review_id');
		$testimonyTable=$this->getServiceLocator()->get('Users\Model\TestimonialsFactory');
		$total = $testimonyTable->getcount($_POST);
		$paginator = $testimonyTable->gettestinomialsGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->review_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->username;
					}
					if($i=='2'){
						$output_item = $result['rResult'][$m]->review_text;
					}
					if($result['rResult'][$m]->status == 0){
						if($i=='4'){
							$output_item ='Edit';
						}
					}else{
						if($i=='4'){
							$output_item ='<a href='.$baseUrl.'admin/edit-testimony?testimony_id='.$result['rResult'][$m]->review_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;	
	}
	public function addUserAction()
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_POST['inputEmail']) && $_POST['inputEmail']!=""){
			$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
			$userdeailsTable=$this->getServiceLocator()->get('Users\Model\UserdetailsFactory');	
			$getuserId=$usersTable->addUser($_POST,'regByadmin');
			if($getuserId!=0){
				$add_status=$userdeailsTable->addUserdetails($getuserId,$_POST);
				if($add_status!=0){
						global $regSubject;				
						global $regMessage;
						$regMessage = str_replace("<FULLNAME>",$_POST['inputUserName'], $regMessage);
						$regMessage = str_replace("<EMAIL>",$_POST['inputEmail'], $regMessage);
						$regMessage = str_replace("<PASSWORD>",$_POST['password'], $regMessage);
						$to=$_POST['inputEmail'];
						$sentMail=sendMail($to,$regSubject,$regMessage);
						if($sentMail>0){
							return $this->redirect()->toUrl($baseUrl.'admin/user-lists');
						}else{
							return $this->redirect()->toUrl($baseUrl.'admin/user-lists');
						}
					//return $this->redirect()->toUrl($baseUrl.'admin/user-lists');
				}
			}
		}else{
			$usertypeTable=$this->getServiceLocator()->get('Users\Model\UsersTypeFactory');
			$getAllusertypes=$usertypeTable->getAllusertypes();
			$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');
			$districtsAll=$districtTable->allDistricts();
			$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
			$areaByDistricts=$areaTable->allAreas();
			$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
			$locationsByArea=$locationTable->allLocations();
			return new ViewModel(array(
				'allUsertypes' 	=> $getAllusertypes,
				'alldistricts' 	=> $districtsAll,
				'areaByDistricts' 	=> $areaByDistricts,
				'locationsByArea' 	=> $locationsByArea,
			));
		}
	}
	public function userListsAction(){
		$usertypeTable=$this->getServiceLocator()->get('Users\Model\UsersTypeFactory');
		$getAllusertypes=$usertypeTable->getAllusertypes();
		return new ViewModel(array(
			'allUsertypes' 	=> $getAllusertypes,
		));
	}	
	public function totaluserlistsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_GET['user_type_id']) && $_GET['user_type_id']!=""){
			$user_type_id=$_GET['user_type_id'];	
		}else{
			$user_type_id='';
		}
		$searchkey='';
		$result=array();
		$sOutput='';			
		$aColumns = array('user_id','user_name','u_status','user_id');
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		$total = $usersTable->getcount($_POST);
		$paginator = $usersTable->getuserlistsGrid($searchkey,$aColumns,$_POST,$user_type_id,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->user_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->user_name;
					}
					if($result['rResult'][$m]->u_status == 0){
						if($i=='3'){
							$output_item ='Edit';
						}
					}else{
						if($i=='3'){
							$output_item ='<a href='.$baseUrl.'admin/edit-user?user_id='.$result['rResult'][$m]->user_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='u_status'){ 
						if($result['rResult'][$m]->u_status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->u_status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;	
	}
	public function addPeriodPackageAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$packagesperoidTable=$this->getServiceLocator()->get('Category\Model\PeriodOfPackagesFactory');	
		if(isset($_POST['hid_period']) && $_POST['hid_period']!=""){
			$add_success = $packagesperoidTable->addPackageperiod($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/period-packages');
			}else{
				return $this->redirect()->toUrl($baseUrl.'admin/period-packages');
			}
		}elseif(isset($_POST['period_name']) && $_POST['period_name']!=''){					
			$add_success = $packagesperoidTable->addPackageperiod($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/period-packages');
			}else{
				return new ViewModel(array(
					'output' 	=> 'not success',
				));
			}
        }else if(isset($_GET['period_pakage_id']) && $_GET['period_pakage_id']!=''){
			$edit_details = $packagesperoidTable->getperiodpackage($_GET['period_pakage_id']);
			if(count($edit_details)>0){
				return new ViewModel(array(
					'package_period_details' 	=> $edit_details,
				));
			}
		}	
	}
	public function totalperiodpackagesAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';			
		$aColumns = array('period_pakage_id','period_name','time_period_amount','status','period_pakage_id');
		$packagesperoidTable=$this->getServiceLocator()->get('Category\Model\PeriodOfPackagesFactory');	
		$total = $packagesperoidTable->getcount($_POST);
		$paginator = $packagesperoidTable->getpackageperiodGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->period_pakage_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->period_name;
					}
					if($i=='2'){
						$output_item = $result['rResult'][$m]->time_period_amount;
					}
					if($result['rResult'][$m]->status == 0){
						if($i=='4'){
							$output_item ='Edit';
						}
					}else{
						if($i=='4'){
							$output_item ='<a href='.$baseUrl.'admin/add-period-package?period_pakage_id='.$result['rResult'][$m]->period_pakage_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function addNumberwordsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$numwordsTable=$this->getServiceLocator()->get('Category\Model\NumberWordsFactory');	
		if(isset($_POST['form_num']) && $_POST['form_num']!=""){
			$from_to_val=$_POST['form_num'].'-'.$_POST['to_num'];
			$already_Exitsts = $numwordsTable->checkFromToValues($from_to_val);
			if($already_Exitsts!=0){
				$result = new JsonModel(array(
					'output' => 'success',
				));	
			}else{
				$result = new JsonModel(array(
					'output' => 'notsuccess',
				));
			}
			return $result;
		}elseif(isset($_POST['hid_num']) && $_POST['hid_num']!=""){
			$from_to=$_POST['num_words'].'-'.$_POST['to_num_words'];
			$add_success = $numwordsTable->addWords($from_to,$_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/number-words');
			}else{
				return $this->redirect()->toUrl($baseUrl.'admin/number-words');
			}
		}elseif(isset($_POST['num_words']) && $_POST['num_words']!=''){	
			$from_to=$_POST['num_words'].'-'.$_POST['to_num_words'];
			$add_success = $numwordsTable->addWords($from_to,$_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/number-words');
			}else{
				return new ViewModel(array(
					'output' 	=> 'not success',
				));
			}
        }else if(isset($_GET['num_word_id']) && $_GET['num_word_id']!=''){
			$edit_details = $numwordsTable->getallWords($_GET['num_word_id']);
			if(count($edit_details)>0){
				$base_range = $numwordsTable->baseRange();
				if(isset($base_range) && $base_range!='' && count($base_range)>0){	
					return new ViewModel(array(
					'check_base_range' 	=> $base_range->base_range,
					'base_range_exists' => 'true',
					'num_words_details' 	=> $edit_details,
					));	
				}else{
					return new ViewModel(array(
					'check_base_range' 	=> '',
					'base_range_exists' => 'false',
					'num_words_details' 	=> $edit_details,
					));	
				}				
			}
		}else{
			$base_range = $numwordsTable->baseRange();
			if(isset($base_range) && $base_range!='' && count($base_range)>0){	
				return new ViewModel(array(
					'check_base_range' 	=> $base_range->base_range,
					'base_range_exists' => 'true',
				));	
			}else{
				return new ViewModel(array(
					'check_base_range' 	=> '',
					'base_range_exists' => 'false',
				));	
			}			
		}	
	}
	public function totalnumberofwordsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';			
		$aColumns = array('num_word_id','num_words','words_amount','status','num_word_id');
		$numwordsTable=$this->getServiceLocator()->get('Category\Model\NumberWordsFactory');	
		$total = $numwordsTable->getcount($_POST);
		$paginator = $numwordsTable->getnumwordsGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->num_word_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->num_words;
					}
					if($i=='2'){
						$output_item = $result['rResult'][$m]->words_amount;
					}	
					if($result['rResult'][$m]->status == 0){
						if($i=='4'){
							$output_item ='Edit';
						}
					}else{
						if($i=='4'){
							$output_item ='<a href='.$baseUrl.'admin/add-numberwords?num_word_id='.$result['rResult'][$m]->num_word_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function locationListAction(){
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');
		$allDistricts = $districtTable->allDistricts();	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$areaLists = $areaTable->allAreas();
		return new ViewModel(array(
			'allDistricts' 	    => $allDistricts,				
			'allAreas' 	        => $areaLists,				
		));	
	}
	public function addLocationsAction(){ 
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		if(isset($_POST['hid_location']) && $_POST['hid_location']!=""){
			$add_location = $locationTable->addLocation($_POST,$_POST['loc_name']);
			if($add_location>0){
				return $this->redirect()->toUrl($baseUrl.'admin/location-list');
			}
		}elseif(isset($_POST['districts']) && $_POST['districts']!=""){
			foreach($_POST['loc_name'] as $key=>$loc_name){
				$add_location = $locationTable->addLocation($_POST,$loc_name);						
			}
			if($add_location>0){
				return $this->redirect()->toUrl($baseUrl.'admin/location-list');
			}
		}elseif(isset($_GET['location_id']) && $_GET['location_id']!=""){
			$getLocation = $locationTable->getLocationDetail($_GET['location_id']);
			$districtLists = $districtTable->allDistricts();
			$areaLists = $areaTable->allAreas();
			return new ViewModel(array(
				'list_districts' 	    => $districtLists,				
				'list_areas' 	        => $areaLists,				
				'get_location' 	        => $getLocation,				
			));	
		}else{
			$districtLists = $districtTable->allDistricts();
			$areaLists = $areaTable->allAreas();
			return new ViewModel(array(
				'list_districts' 	        => $districtLists,				
				'list_areas' 	        => $areaLists,				
			));	
		}	
	}
	public function totallocationsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';		
		if(isset($_GET['dist_id']) && $_GET['dist_id']!=""){
			$distId=$_GET['dist_id'];
		}else{
			$distId='';
		}
		if(isset($_GET['area_id']) && $_GET['area_id']!=""){
			$areaId=$_GET['area_id'];
		}else{
			$areaId='';
		}
		$aColumns = array('location_id','location_name','status','location_id');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		$total = $locationTable->getcount($_POST);
		$paginator = $locationTable->getlocationsGrid($searchkey,$aColumns,$_POST,$distId,$areaId,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->location_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->location_name;
					}					
					
					if($result['rResult'][$m]->status == 0){
						if($i=='3'){
							$output_item ='Edit';
						}
					}else{
						if($i=='3'){
							$output_item ='<a href='.$baseUrl.'admin/add-locations?location_id='.$result['rResult'][$m]->location_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function addAreaAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		if(isset($_POST['area_id']) && $_POST['area_id']!=""){
			$add_area = $areaTable->addAreas($_POST['area'],$_POST['districts'],$_POST['area_id']);	
			if($add_area>0){
				return $this->redirect()->toUrl($baseUrl.'admin/area-lists');
			}
		}elseif(isset($_POST['districts']) && $_POST['districts']!=""){
			foreach($_POST['area'] as $key=>$area_names){
				$add_area = $areaTable->addAreas($area_names,$_POST['districts'],null);				
			}
			if($add_area>0){
				return $this->redirect()->toUrl($baseUrl.'admin/area-lists');
			}
		}elseif(isset($_GET['area_id']) && $_GET['area_id']!=""){				
			$areas = $areaTable->editAreas($_GET['area_id']);	
			return new ViewModel(array(
				'areas_details' => $areas,
			));	
		}else{
			$districtLists = $districtTable->allDistricts();
			return new ViewModel(array(
				'list_districts' 	        => $districtLists,				
			));	
		}	
	}
	public function addDistrictAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		if(isset($_POST['hid_dist']) && $_POST['hid_dist']!=""){
			$add_success = $districtTable->addDistrict($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/district-lists');
			}else{
				return $this->redirect()->toUrl($baseUrl.'admin/district-lists');
			}
		}elseif(isset($_POST['dist_name']) && $_POST['dist_name']!=''){					
			$add_success = $districtTable->addDistrict($_POST);
			if($add_success!=0){
				return $this->redirect()->toUrl($baseUrl.'admin/district-lists');
			}else{
				return new ViewModel(array(
					'output' 	=> 'not success',
				));
			}
        }else if(isset($_GET['district_id']) && $_GET['district_id']!=''){
			$edit_details = $districtTable->getallDistrict($_GET['district_id']);
			if(count($edit_details)>0){
				return new ViewModel(array(
					'dist_details' 	=> $edit_details,
				));
			}
		}	
	}
	public function districtListsAction(){
	
	}	
	public function totaldistrictsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';
		$aColumns = array('district_id','district_name','status','district_id');
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$total = $districtTable->getcount($_POST);
		$paginator = $districtTable->getdistrictlistGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->district_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->district_name;
					}
					if($result['rResult'][$m]->status == 0){
						if($i=='3'){
							$output_item ='Edit';
						}
					}else{
						if($i=='3'){
							$output_item ='<a href='.$baseUrl.'admin/add-district?district_id='.$result['rResult'][$m]->district_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function areaListsAction(){
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');
		$allDistricts = $districtTable->allDistricts();	
		return new ViewModel(array(
			'allDistricts' 	        => $allDistricts,				
		));		
	}
	public function totalarealistsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_GET['dist_id']) && $_GET['dist_id']!=""){
			$distId=$_GET['dist_id'];
		}else{
			$distId='';
		}
		$searchkey='';
		$result=array();
		$sOutput='';
		$aColumns = array('area_id','area_name','status','area_id');
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$total = $areaTable->getcount($_POST);
		$paginator = $areaTable->getareaslistGrid($searchkey,$aColumns,$_POST,$distId,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->area_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->area_name;
					}	
					if($result['rResult'][$m]->status == 0){
						if($i=='3'){
							$output_item ='Edit';
						}
					}else{
						if($i=='3'){
							$output_item ='<a href='.$baseUrl.'admin/add-area?area_id='.$result['rResult'][$m]->area_id.'  title="Edit" class="">Edit</a>';
						}	
					}					
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function totalcategorieslistAction()
	{
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';
		$aColumns = array('category_id','category_name','status','category_id','category_id');
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');	
		$subcategoryTable=$this->getServiceLocator()->get('Category\Model\SubCategoryFactory');
		$total = $categoryTable->getcount($_POST);
		$paginator = $categoryTable->getcategorieslistGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->category_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->category_name;
					}					
					if($i=='3'){
						if($result['rResult'][$m]->status != 0){
							$output_item ='<a href='.$baseUrl.'admin/add-category?category_id='.$result['rResult'][$m]->category_id.'  title="Edit" class="">Edit</a>';
						}else{
							$output_item ='Edit';
						}
					}
					$count_status = $subcategoryTable->checkCategory($result['rResult'][$m]->category_id);
					if($count_status>0){
						if($i=='4'){
							if($result['rResult'][$m]->status != 0){
								$output_item ='<a href='.$baseUrl.'admin/subcategory?category_id='.$result['rResult'][$m]->category_id.'  title="Sub-categories" class="">Sub categories</a>';
							}else{
								$output_item ='Sub Categories';
							}
						}
					}else{
						if($i=='4'){
							if($result['rResult'][$m]->status != 0){
								$output_item ='<a href='.$baseUrl.'admin/subcategory?category_id='.$result['rResult'][$m]->category_id.'  title="Sub-categories" class="">Add-subcategories</a>';
							}else{
								$output_item ='Add-subcategories';
							}
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function inactivateAction()
	{	
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');
		$adstypeTable=$this->getServiceLocator()->get('Category\Model\AddTypeFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		$paperTable=$this->getServiceLocator()->get('Category\Model\PaperCriculationFactory');
		$numwordsTable=$this->getServiceLocator()->get('Category\Model\NumberWordsFactory');
		$packagesperoidTable=$this->getServiceLocator()->get('Category\Model\PeriodOfPackagesFactory');	
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		$testimonyTable=$this->getServiceLocator()->get('Users\Model\TestimonialsFactory');
		$offersTable=$this->getServiceLocator()->get('Users\Model\OffersFactory');
		$radiusTable=$this->getServiceLocator()->get('Category\Model\RadiusFactory');
		$dayamountsTable=$this->getServiceLocator()->get('Category\Model\DayAmountsFactory');
		$imagedimensionsTable=$this->getServiceLocator()->get('Category\Model\ImageDimensionsFactory');
		$sentnotificationTable=$this->getServiceLocator()->get('Users\Model\SentNotificationsFactory');
		$orderstatusTable=$this->getServiceLocator()->get('Users\Model\OrdersStatusFactory');
		$ordermainTable=$this->getServiceLocator()->get('Users\Model\OrderMainsFactory');
		$ids='';
		$items = rtrim($_POST['items'],",");
		$newItems = '';
		$itemsArray = explode(',',$items);
		foreach($itemsArray as $itemsValues)
		{
			$newItems.= base64_decode($itemsValues).',';
		}
		$id = rtrim($newItems,",");
		$idds=explode(',',$id);
		for($i=0;$i<count($idds);$i++){
			$ids .= "'".$idds[$i]."'".",";
		}
		$tIds = rtrim($ids,",");
		if(isset($_POST['type'])){
			if($_POST['type']=='cat'){					
				$result = $categoryTable->changeStatus($tIds,0);		
			}elseif($_POST['type']=='dist'){
				$result = $districtTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='ads_types'){
				$result = $adstypeTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='area'){
				$result = $areaTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='locations'){
				$result = $locationTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='paper'){
				$result = $paperTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='numbers_words'){
				$result = $numwordsTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='period_packages'){
				$result = $packagesperoidTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='users'){
				$result = $usersTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='testimony'){
				$result = $testimonyTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='offer'){
				$result = $offersTable->changeStatus($tIds,0);
				$change_sent_notification = $sentnotificationTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='radius'){
				$result = $radiusTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='day'){
				$result = $dayamountsTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='dimension'){
				$result = $imagedimensionsTable->changeStatus($tIds,0);
			}elseif($_POST['type']=='orders'){
				$result1 = $orderstatusTable->changeStatus($tIds,0);
				$result = $ordermainTable->changeStatus($tIds,0);
			}
			
		}
		$result = new JsonModel(array(
			'total' => $result,
		));	
		$result->setTerminal(true);
		return $result; 
	}
	public function activateAction()
	{
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$adstypeTable=$this->getServiceLocator()->get('Category\Model\AddTypeFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		$paperTable=$this->getServiceLocator()->get('Category\Model\PaperCriculationFactory');
		$numwordsTable=$this->getServiceLocator()->get('Category\Model\NumberWordsFactory');
		$packagesperoidTable=$this->getServiceLocator()->get('Category\Model\PeriodOfPackagesFactory');	
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		$testimonyTable=$this->getServiceLocator()->get('Users\Model\TestimonialsFactory');
		$offersTable=$this->getServiceLocator()->get('Users\Model\OffersFactory');
		$radiusTable=$this->getServiceLocator()->get('Category\Model\RadiusFactory');
		$dayamountsTable=$this->getServiceLocator()->get('Category\Model\DayAmountsFactory');
		$imagedimensionsTable=$this->getServiceLocator()->get('Category\Model\ImageDimensionsFactory');
		$sentnotificationTable=$this->getServiceLocator()->get('Users\Model\SentNotificationsFactory');
		$orderstatusTable=$this->getServiceLocator()->get('Users\Model\OrdersStatusFactory');
		$ordermainTable=$this->getServiceLocator()->get('Users\Model\OrderMainsFactory');
		$ids='';
		$items = rtrim($_POST['items'],",");
		$newItems = '';
		$itemsArray = explode(',',$items);
		foreach($itemsArray as $itemsValues)
		{
			$newItems.= base64_decode($itemsValues).',';
		}		
		$id = rtrim($newItems,",");
		$idds=explode(',',$id);
		for($i=0;$i<count($idds);$i++){
			$ids .= "'".$idds[$i]."'".",";
		}
		$tIds = rtrim($ids,",");
		if(isset($_POST['type'])){
			if($_POST['type']=='cat'){				
				$result = $categoryTable->changeStatus($tIds,1);			
			}elseif($_POST['type']=='dist'){
				$result = $districtTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='ads_types'){
				$result = $adstypeTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='area'){
				$result = $areaTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='locations'){
				$result = $locationTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='paper'){
				$result = $paperTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='numbers_words'){
				$result = $numwordsTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='period_packages'){
				$result = $packagesperoidTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='users'){
				$result = $usersTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='testimony'){
				$result = $testimonyTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='offer'){
				$result = $offersTable->changeStatus($tIds,1);
				$change_sent_notification = $sentnotificationTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='radius'){
				$result = $radiusTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='day'){
				$result = $dayamountsTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='dimension'){
				$result = $imagedimensionsTable->changeStatus($tIds,1);
			}elseif($_POST['type']=='orders'){
				$result1 = $orderstatusTable->changeStatus($tIds,1);
				$result = $ordermainTable->changeStatus($tIds,1);
			}
		}
		$result = new JsonModel(array(
			'total' => $result,
		));	
		$result->setTerminal(true);
		return $result; 
	}
	public function getAreasAction(){
		if(isset($_POST['dist_id']) && $_POST['dist_id']!=""){
			$dist_id=$_POST['dist_id'];
			if(isset($_POST['areas_id']) && $_POST['areas_id']!=""){
				$uarea_id=$_POST['areas_id'];
			}else{
				$uarea_id='';
			}	
			if(isset($_POST['sess_area_id']) && $_POST['sess_area_id']!=''){
				$ses_area_id=$_POST['sess_area_id'];
			}else{
				$ses_area_id='';
			}			
			$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
			$getAreas=$areaTable->getAreas($dist_id);
			$html="";
			$html.='<option value="">Select a area</option>';	
			foreach($getAreas as $area){
				if($uarea_id!=""){
					if($uarea_id==$area->area_id){
						$html.='<option value="'.$area->area_id.'" selected="selected">'.$area->area_name.'</option>';					
					}else{
						$html.='<option value="'.$area->area_id.'">'.$area->area_name.'</option>';
					}
				}if($ses_area_id!=""){
					if($ses_area_id==$area->area_id){
						$html.='<option value="'.$area->area_id.'" selected="selected">'.$area->area_name.'</option>';					
					}else{
						$html.='<option value="'.$area->area_id.'">'.$area->area_name.'</option>';
					}
				}else{
					$html.='<option value="'.$area->area_id.'">'.$area->area_name.'</option>';
				}
			}			
		}else{
			$html="";
			$html.='<option value="">Select a area</option>';		
		}
		$result = new JsonModel(array(					
			'output' 		=> 'success',
			'list_areas'	=>	$html,
		));	
		return $result;	
	}
	public function paperPopulationAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$addPaper=0;
		$paper_info=array();
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		$paperTable=$this->getServiceLocator()->get('Category\Model\PaperCriculationFactory');
		$mainpaperTable=$this->getServiceLocator()->get('Category\Model\PaperFactory');
		if(isset($_POST['hid_paper']) && $_POST['hid_paper']!=""){
			$addPaper = $paperTable->addPaper($_POST['paper_name'],$_POST['cnt_paper'],$_POST['amt_paper'],$_POST);
			if($addPaper >= 0){
				return $this->redirect()->toUrl($baseUrl.'admin/population-paper');
			}
		}elseif(isset($_POST['districts']) && $_POST['districts']!=""){	
			$paper_num=0;
			foreach($_POST['paper_name'] as $key=>$paper_name){ $paper_info[$paper_num]['paper_name'] = $paper_name; $paper_num++;}
			$paper_num=0;
			foreach($_POST['cnt_paper'] as $keyy=>$paper_count){ $paper_info[$paper_num]['paper_count'] = $paper_count;$paper_num++;}
			$paper_num=0;
			foreach($_POST['amt_paper'] as $keyyy=>$paper_amt_count){ $paper_info[$paper_num]['paper_amt_count'] = $paper_amt_count;$paper_num++;}
			foreach($paper_info as $paperInfo){
				$addPaper = $paperTable->addPaper($paperInfo['paper_name'],$paperInfo['paper_count'],$paperInfo['paper_amt_count'],$_POST);	
			}			
			if($addPaper > 0){
				return $this->redirect()->toUrl($baseUrl.'admin/population-paper');
			}
		}elseif(isset($_GET['paper_cir_id']) && $_GET['paper_cir_id']!=""){ 
			$districtLists = $districtTable->allDistricts();
			$areaLists = $areaTable->allAreas();
			$locationLists = $locationTable->allLocations();
			$paperList = $mainpaperTable->allPapers();
			$info_paper = $paperTable->getPaperInfo($_GET['paper_cir_id']);	
			return new ViewModel(array(
				'list_districts' 	    => $districtLists,				
				'list_areas' 	        => $areaLists,				
				'list_location' 	    => $locationLists,				
				'get_paper' 	        => $info_paper,				
				'paper_list' 	        => $paperList,				
			));	
		}else{		
			$districtLists = $districtTable->allDistricts();
			$areaLists = $areaTable->allAreas();
			$locationLists = $locationTable->allLocations();
			$paperList = $mainpaperTable->allPapers();
			return new ViewModel(array(
				'list_districts' 	    => $districtLists,				
				'list_areas' 	        => $areaLists,				
				'list_location' 	    => $locationLists,
				'paper_list' 	        => $paperList,	
			));	
		}
	}
	public function populationPaperAction(){
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		$mainpaperTable=$this->getServiceLocator()->get('Category\Model\PaperFactory');
		$districtLists = $districtTable->allDistricts();
		$areaLists = $areaTable->allAreas();
		$locationLists = $locationTable->allLocations();
		$paperList = $mainpaperTable->allPapers();
		return new ViewModel(array(
			'list_districts' 	    => $districtLists,
			'paper_list' 	        => $paperList,				
		));	
	}
	public function totalpapersAction(){ 
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';		
		if(isset($_GET['dist_id']) && $_GET['dist_id']!=""){
			$distId=$_GET['dist_id'];
		}else{
			$distId='';
		}
		if(isset($_GET['area_id']) && $_GET['area_id']!=""){
			$areaId=$_GET['area_id'];
		}else{
			$areaId='';
		}
		if(isset($_GET['location_id']) && $_GET['location_id']!=""){
			$locationId=$_GET['location_id'];
		}else{
			$locationId='';
		}
		if(isset($_GET['paper_id']) && $_GET['paper_id']!=""){
			$paperId=$_GET['paper_id'];
		}else{
			$paperId='';
		}
		$aColumns = array('paper_cir_id','paper_name','status','paper_cir_id');
		$paperTable=$this->getServiceLocator()->get('Category\Model\PaperCriculationFactory');
		$total = $paperTable->getcount($_POST);
		$paginator = $paperTable->getpaperGrid($searchkey,$aColumns,$_POST,$distId,$areaId,$locationId,$paperId,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->paper_cir_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->paper_name;
					}					
					if($i=='3'){
						if($result['rResult'][$m]->status == 0){
							$output_item ='Edit';
						}else{
							$output_item ='<a href='.$baseUrl.'admin/paper-population?paper_cir_id='.$result['rResult'][$m]->paper_cir_id.'  title="Edit" class="">Edit</a>';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function getLocationsAction(){
		if(isset($_POST['dist_id']) && $_POST['dist_id']){
			if(isset($_POST['location_id']) && $_POST['location_id']!=""){
				$ulocationId=$_POST['location_id'];
			}else{
				$ulocationId='';
			}
			if(isset($_POST['ses_location_id']) && $_POST['ses_location_id']!=''){
				$ses_loc_id=$_POST['ses_location_id'];
			}else{
				$ses_loc_id='';
			}
			$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
			$locationLists = $locationTable->getAreaBasedLoations($_POST);
			$html="";
			$html.='<option value="">Select a location</option>';	
			foreach($locationLists as $location){
				if($ulocationId!=""){
					if($ulocationId==$location->location_id){
						$html.='<option value="'.$location->location_id.'" selected="selected">'.$location->location_name.'</option>';
					}
					else{
						$html.='<option value="'.$location->location_id.'">'.$location->location_name.'</option>';
					}
				}
				if($ses_loc_id!=''){
					if($ses_loc_id==$location->location_id)
					{
						$html.='<option value="'.$location->location_id.'" selected="selected">'.$location->location_name.'</option>';
					}
					else
					{
						$html.='<option value="'.$location->location_id.'">'.$location->location_name.'</option>';
					}
				}
				else{
					$html.='<option value="'.$location->location_id.'">'.$location->location_name.'</option>';
				}
			}			
		}else{
			$html="";
			$html.='<option value="">Select a location</option>';		
		}
		$result = new JsonModel(array(					
			'output' 		=> 'success',
			'loca_list'	=>	$html,
		));	
		return $result;	
	}
	public function forgetpasswordAction(){}
	public function updatepasswordAction(){
		$token=$_GET['token'];
		$tokeninfo=array();
		$exitToke=0;
		$forgetTable=$this->getServiceLocator()->get('Users\Model\ForgetpasswordFactory');
		$tokenExit=$forgetTable->gettoken($token);		
		if(count($tokenExit)!=0){		
			foreach($tokenExit as $tokeninfo){}				
				if($tokeninfo->forget_id > 0){
					$exitToken=1;
				}else{
					$exitToken=0;
				}
		}else{
			$exitToken=0;
		}
		$viewModel = new ViewModel(
			array('existtoken'=>$exitToken)
		);	
		return $viewModel;			
	}
	public function editUserAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');	
		$userdeailsTable=$this->getServiceLocator()->get('Users\Model\UserdetailsFactory');		
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!=""){
			$update_user_status = $usersTable->updateUser($_POST);
			if($update_user_status>=0){
				$update_userdetails_status = $userdeailsTable->updateUserDetails($_POST);
				if($update_userdetails_status>=0){
					return $this->redirect()->toUrl($baseUrl.'admin/user-lists');
				}
			}
		}else if(isset($_GET['user_id']) && $_GET['user_id']){
			$userdetails = $usersTable->getuserDetailes($_GET['user_id']);
			$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');
			$districtsAll=$districtTable->allDistricts();
			$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
			$areasAll=$areaTable->allAreas();
			$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
			$locationsAll=$locationTable->allLocations();
			$result = new ViewModel(array(
				'userdetails'=>$userdetails,
				'alldistricts' 	=> $districtsAll,
				'areasAll' 	=> $areasAll,
				'locationsAll' 	=> $locationsAll,
				
			));	
			return $result;	
		}
	}
	public function addOffersAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$offersTable=$this->getServiceLocator()->get('Users\Model\OffersFactory');
		$sentnotificationTable=$this->getServiceLocator()->get('Users\Model\SentNotificationsFactory');
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		// updating offer..
		if(isset($_POST['hid_offer_id']) && $_POST['hid_offer_id']!=""){
			if(isset($_POST['exp_check']) && $_POST['exp_check']!=""){
				$exp_status='1';
			}else{
				$exp_status='0';
			}
			$add_offer_status = $offersTable->addoffer($_POST,$exp_status);
			if($add_offer_status>=0){
				return $this->redirect()->toUrl($baseUrl.'admin/offer-lists');
			}
		// adding offer..
		}else if(isset($_POST['offer_name']) && $_POST['offer_name']){	
			if(isset($_POST['exp_check']) && $_POST['exp_check']!=""){
				$exp_status='1';
			}else{
				$exp_status='0';
			}
			$add_offer_status = $offersTable->addoffer($_POST,$exp_status);
			if($add_offer_status!="0"){	
				$pendingmails=$usersTable->getallusersemails();
				if($pendingmails->count()>0)
					{						
						foreach($pendingmails as $mailcontent)
						{
							$user_id=$mailcontent->user_id;
							$add_sent_notified_status=$sentnotificationTable->addsentNotified($user_id,$add_offer_status);
						}
						return $this->redirect()->toUrl($baseUrl.'admin/offer-lists');
					}
				else
					{
						return $this->redirect()->toUrl($baseUrl.'admin/offer-lists');
					}
			}
		// editing offer.. 
		}else if(isset($_GET['offer_id']) && $_GET['offer_id']){
			$offer_details = $offersTable->editoffer($_GET['offer_id']);
			$result = new ViewModel(array(
				'offer_details'=>$offer_details,
			));	
			return $result;	
		}
	}
	public function offerListsAction(){}
	public function totalofferslistsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';		
		$aColumns = array('offer_id','offer_name','offer_amount','percent_discount','status','offer_id');
		$offersTable=$this->getServiceLocator()->get('Users\Model\OffersFactory');
		$total = $offersTable->getcount($_POST);
		$paginator = $offersTable->getoffersGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->offer_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->offer_name;
					}	
					if($i=='2'){
						$output_item = $result['rResult'][$m]->offer_amount;
					}	
					if($i=='3'){
						$output_item = $result['rResult'][$m]->percent_discount;
					}	
					if($i=='5'){
						if($result['rResult'][$m]->status == 1){
							$output_item ='<a href='.$baseUrl.'admin/add-offers?offer_id='.$result['rResult'][$m]->offer_id.'  title="Edit" class="">Edit</a>';
						}else{
							$output_item ='Edit';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;
	}
	public function radiusAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$radiusTable=$this->getServiceLocator()->get('Category\Model\RadiusFactory');
		if(isset($_POST['hid_radius']) && $_POST['hid_radius']!=""){
			$update_status=$radiusTable->addRadius($_POST);
			if($update_status>=0){
				return $this->redirect()->toUrl($baseUrl.'admin/radius-lists');
			}
		}else if(isset($_POST['distance']) && $_POST['distance']!=""){
			$add_status=$radiusTable->addRadius($_POST);
			if($add_status>0){
				return $this->redirect()->toUrl($baseUrl.'admin/radius-lists');
			}
		}else if(isset($_GET['radius_id']) && $_GET['radius_id']!=""){
			$getRadius_info=$radiusTable->getRadiusInfo($_GET['radius_id']);
			$result = new ViewModel(array(
				'radius_details'=>$getRadius_info,
			));	
			return $result;	
		}
	}
	public function radiusListsAction(){}
	public function totalradiuslistAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';		
		$aColumns = array('radius_id','distance','amount_for_disatnce','status','radius_id');
		$radiusTable=$this->getServiceLocator()->get('Category\Model\RadiusFactory');
		$total = $radiusTable->getcount($_POST);
		$paginator = $radiusTable->getradiuslistGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->radius_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->distance.'km(s)';
					}	
					if($i=='2'){
						$output_item = $result['rResult'][$m]->amount_for_disatnce;
					}					
					if($i=='4'){
						if($result['rResult'][$m]->status == 1){
							$output_item ='<a href='.$baseUrl.'admin/radius?radius_id='.$result['rResult'][$m]->radius_id.'  title="Edit" class="">Edit</a>';
						}else{
							$output_item ='Edit';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;		
	}
	public function dayAmountsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$dayamountsTable=$this->getServiceLocator()->get('Category\Model\DayAmountsFactory');
		if(isset($_POST['hid_day']) && $_POST['hid_day']!=""){
			$update_status=$dayamountsTable->addDayAmount($_POST);
			if($update_status>=0){
				return $this->redirect()->toUrl($baseUrl.'admin/dayamount-lists');
			}
		}else if(isset($_POST['day_type']) && $_POST['day_type']!=""){
			$add_status=$dayamountsTable->addDayAmount($_POST);
			if($add_status>0){
				return $this->redirect()->toUrl($baseUrl.'admin/dayamount-lists');
			}
		}else if(isset($_GET['day_id']) && $_GET['day_id']!=""){
			$getdayamount_info=$dayamountsTable->getDayAmounts($_GET['day_id']);
			$result = new ViewModel(array(
				'daytypes_details'=>$getdayamount_info,
			));	
			return $result;	
		}
	}
	public function dayamountListsAction(){}
	public function totaldaysamountlistAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';		
		$aColumns = array('day_id','day_type','day_type_amount','status','day_id');
		$dayamountsTable=$this->getServiceLocator()->get('Category\Model\DayAmountsFactory');
		$total = $dayamountsTable->getcount($_POST);
		$paginator = $dayamountsTable->getdayamountlistGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->day_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->day_type;
					}	
					if($i=='2'){
						$output_item = $result['rResult'][$m]->day_type_amount;
					}					
					if($i=='4'){
						if($result['rResult'][$m]->status == 1){
							$output_item ='<a href='.$baseUrl.'admin/day-amounts?day_id='.$result['rResult'][$m]->day_id.'  title="Edit" class="">Edit</a>';
						}else{
							$output_item ='Edit';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;	
	}
	public function imageDimensionsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$imagedimensionsTable=$this->getServiceLocator()->get('Category\Model\ImageDimensionsFactory');
		if(isset($_POST['hid_image_dimension']) && $_POST['hid_image_dimension']!=""){
			$width_height_val=$_POST['image_dimension_w'].'*'.$_POST['image_dimension_h'];
			$update_status=$imagedimensionsTable->addImageDimension($_POST,$width_height_val);
			if($update_status>=0){
				return $this->redirect()->toUrl($baseUrl.'admin/image-dimension-lists');
			}
		}else if(isset($_POST['image_dimension_w']) && $_POST['image_dimension_w']!=""){
			$width_height_val=$_POST['image_dimension_w'].'*'.$_POST['image_dimension_h'];
			$add_status=$imagedimensionsTable->addImageDimension($_POST,$width_height_val);
			if($add_status>0){
				return $this->redirect()->toUrl($baseUrl.'admin/image-dimension-lists');
			}
		}else if(isset($_GET['dimension_id']) && $_GET['dimension_id']!=""){
			$getdimensions_info=$imagedimensionsTable->getDimensions($_GET['dimension_id']);
			$result = new ViewModel(array(
				'imagedimension_details'=>$getdimensions_info,
			));	
			return $result;	
		}
	}
	public function imageDimensionListsAction(){}
	public function totalimagedimensionlistsAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$searchkey='';
		$result=array();
		$sOutput='';		
		$aColumns = array('image_dimension_id','image_dimension','dimension_amount','status','image_dimension_id');
		$imagedimensionsTable=$this->getServiceLocator()->get('Category\Model\ImageDimensionsFactory');
		$total = $imagedimensionsTable->getcount($_POST);
		$paginator = $imagedimensionsTable->getimagedimensionlistGrid($searchkey,$aColumns,$_POST,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$output_item = $result['rResult'][$m]->$aColumns[$i];
					if($i=='0'){
						$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->image_dimension_id).'" />';
					}
					if($i=='1'){
						$output_item = $result['rResult'][$m]->image_dimension;
					}	
					if($i=='2'){
						$output_item = $result['rResult'][$m]->dimension_amount;
					}					
					if($i=='4'){
						if($result['rResult'][$m]->status == 1){
							$output_item ='<a href='.$baseUrl.'admin/image-dimensions?dimension_id='.$result['rResult'][$m]->image_dimension_id.'  title="Edit" class="">Edit</a>';
						}else{
							$output_item ='Edit';
						}
					}
					if($aColumns[$i]=='status'){ 
						if($result['rResult'][$m]->status == 1) $output_item = 'Active';
						else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
					}
					$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;	
	}
	public function getPapersAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_POST['location_id']) && $_POST['location_id']!=''){
			$paperTable=$this->getServiceLocator()->get('Category\Model\PaperCriculationFactory');
			$get_paper_list=$paperTable->getPaperList($_POST['dist_id'],$_POST['area_id'],$_POST['location_id']);
			$html="";
			$html.='<option value="">Select a paper</option>';	
			foreach($get_paper_list as $paperInfo){	
				if($_POST['ses_paper_id']==$paperInfo->paper_cir_id){
					$html.='<option value="'.$paperInfo->paper_cir_id.'" selected="selected">'.$paperInfo->paper_name.'</option>';
				}else{
					$html.='<option value="'.$paperInfo->paper_cir_id.'">'.$paperInfo->paper_name.'</option>';
				}	
			}			
		}else{
			$html="";
			$html.='<option value="">Select a paper</option>';		
		}
		$result = new JsonModel(array(					
			'output' 	=> 'success',
			'paper_list'	=>	$html,
		));	
		return $result;	
	}
	public function adPostingAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$langugeTable=$this->getServiceLocator()->get('Category\Model\LanguageFactory');
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
		$districtTable=$this->getServiceLocator()->get('Category\Model\DistrictsFactory');	
		$areaTable=$this->getServiceLocator()->get('Category\Model\AreaFactory');
		$locationTable=$this->getServiceLocator()->get('Category\Model\LocationFactory');
		$radiusTable=$this->getServiceLocator()->get('Category\Model\RadiusFactory');
		$packagesperoidTable=$this->getServiceLocator()->get('Category\Model\PeriodOfPackagesFactory');
		$imagedimensionsTable=$this->getServiceLocator()->get('Category\Model\ImageDimensionsFactory');
		$adstypeTable=$this->getServiceLocator()->get('Category\Model\AddTypeFactory');
		$numwordsTable=$this->getServiceLocator()->get('Category\Model\NumberWordsFactory');
		if(isset($_POST['ses_current_order']) && $_POST['ses_current_order']!=''){
			$update_session_order=$_POST['ses_current_order'];
			$main_array=array();
			$paper_print=array();
			$magazine=array();
			$home_delivery=array();
			foreach($_POST as $key=>$value){
				if(strpos($key,'paper') !== false) {
					if( $_POST['paper_populationpaperdrop'] != ''&& $value!='' )
					{
						$paper_print[$key]=$value;
					}
				}else if(strpos($key,'magazine') !== false){
					if( $_POST['magazine_titleMagazine'] != '' && $value!='')
					{
						$magazine[$key]=$value;
					}
				}else if(strpos($key,'home') !== false){
				if( $_POST['home_titleHome'] != '' && $value!='' )
					{
						$home_delivery[$key]=$value;
					}
				}else{
					$main_array[$key]=$value;
				}
				$_SESSION['orders'][$update_session_order]['Main_array']=$main_array;				
				if(!empty($paper_print)){
					$_SESSION['orders'][$update_session_order]['Print_Paper']=$paper_print;
				}
				if(count($magazine)>0){
					$_SESSION['orders'][$update_session_order]['Magazine']=$magazine;
				}
				if(!empty($home_delivery)){
					$_SESSION['orders'][$update_session_order]['Home_delivery']=$home_delivery;
				} 	
				if( !isset($_POST['paper_print']) ) {
					unset($_SESSION['orders'][$update_session_order]['Print_Paper']);
				}
				if( !isset($_POST['magazine_magazineCheck']) ){
					unset($_SESSION['orders'][$update_session_order]['Magazine']);
				}
				if( !isset($_POST['home_homedelevarycheck']) ){
					unset($_SESSION['orders'][$update_session_order]['Home_delivery']);
				}
			}
			return $this->redirect()->toUrl($baseUrl.'admin/confirm-order');		
		}else if(isset($_POST['language'])){
		    $main_array=array();
			$paper_print=array();
			$magazine=array();
			$home_delivery=array();
			foreach($_POST as $key=>$value){
				if(strpos($key,'paper') !== false) {
					if( $_POST['paper_populationpaperdrop'] != '' && $value!='')
					{
						$paper_print[$key]=$value;
					}
				}else if(strpos($key,'magazine') !== false){
					if( $_POST['magazine_titleMagazine'] != '' && $value!='')
					{
						$magazine[$key]=$value;
					}
				}else if(strpos($key,'home') !== false){
					if( $_POST['home_titleHome'] != '' && $value!='')
					{
						$home_delivery[$key]=$value;
					}
				}else{
					$main_array[$key]=$value;
				}
				
			}		   
			if(isset($_SESSION['orders'])){	
				$orderCount=count($_SESSION['orders']);					
			}else{	
				$orderCount=0;	
				$user_session = new Container('orders');
				$_SESSION['orders']=array();
			}				
				$_SESSION['orders'][$orderCount]['Main_array']=$main_array;				
				if(!empty($paper_print)){
					$_SESSION['orders'][$orderCount]['Print_Paper']=$paper_print;
				}
				if(count($magazine)>0){
					$_SESSION['orders'][$orderCount]['Magazine']=$magazine;
				}
				if(!empty($home_delivery)){
					$_SESSION['orders'][$orderCount]['Home_delivery']=$home_delivery;
				} 
				return $this->redirect()->toUrl($baseUrl.'admin/confirm-order');
		}else if(isset($_GET['currentOrder']) && $_GET['currentOrder']!=''){
			$getLanguges=$langugeTable->allLanguges();
			$getCategories=$categoryTable->getallCategories();
			$districtLists = $districtTable->allDistricts();
			$get_all_radius = $radiusTable->getallRadius();
			$get_all_periods= $packagesperoidTable->allperiodofpackages();
			$get_all_dimensions= $imagedimensionsTable->getallDimensions()->buffer();
			$get_adMode= $adstypeTable->alladstypes()->buffer();
			$base_range = $numwordsTable->baseRange();
			$result = new ViewModel(array(
				'all_languages'           => $getLanguges,
				'all_categories'          => $getCategories,
				'list_districts'          => $districtLists,
				'list_radius'             => $get_all_radius,
				'list_periodfor_pakages'  => $get_all_periods,
				'list_image_dimensions'   => $get_all_dimensions,
				'list_admodes'            => $get_adMode,
				'base_range'              => $base_range,
				'session_data'            => $_SESSION['orders'][$_GET['currentOrder']],		
			));	
			return $result;		
		}else{
			$getLanguges=$langugeTable->allLanguges();
			$getCategories=$categoryTable->getallCategories();
			$districtLists = $districtTable->allDistricts();
			$get_all_radius = $radiusTable->getallRadius();
			$get_all_periods= $packagesperoidTable->allperiodofpackages();
			$get_all_dimensions= $imagedimensionsTable->getallDimensions()->buffer();
			$get_adMode= $adstypeTable->alladstypes()->buffer();
			$base_range = $numwordsTable->baseRange();
			$result = new ViewModel(array(
				'all_languages'           =>$getLanguges,
				'all_categories'          =>$getCategories,
				'list_districts'          => $districtLists,
				'list_radius'             => $get_all_radius,
				'list_periodfor_pakages'   => $get_all_periods,
				'list_image_dimensions'   => $get_all_dimensions,
				'list_admodes'         => $get_adMode,
				'base_range'         => $base_range,
			));	
			return $result;	
		}
	}
	public function confirmOrderAction(){
		$_SESSION['orders']['total_amount']='';
		$_SESSION['orders']['discount_amount']='';
		$_SESSION['orders']['discount_percent']='';
		$sentnotificationTable=$this->getServiceLocator()->get('Users\Model\SentNotificationsFactory');
		$currentDate=date("Y-m-d");
		$get_offers=$sentnotificationTable->getUserOffers('admin_id',$currentDate);
		$result = new ViewModel(array(
			'getUserOffers'=>$get_offers,
		));	
		return $result;
	}
	public function unsetSessionDivAction(){
		if(isset($_POST['session_div']) && $_POST['session_div']!=''){
			$incrValue=$_POST['incr_value'];
			if($_POST['session_div']=='paper'){
				unset($_SESSION['orders'][$incrValue]['Print_Paper']);
			}elseif($_POST['session_div']=='magazine'){
				unset($_SESSION['orders'][$incrValue]['Magazine']);
			}elseif($_POST['session_div']=='home'){
				unset($_SESSION['orders'][$incrValue]['Home_delivery']);
			}
			if(!isset($_SESSION['orders'][$incrValue]['Print_Paper']) && !isset($_SESSION['orders'][$incrValue]['Magazine'])
			&& !isset($_SESSION['orders'][$incrValue]['Home_delivery'])){
				unset($_SESSION['orders'][$incrValue]);
			}
			$result = new JsonModel(array(
				'output' =>'success',
			));	
			return $result;
		}
	}
	public function orderSaveAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$ordermainTable=$this->getServiceLocator()->get('Users\Model\OrderMainsFactory');
		$orderinfoTable=$this->getServiceLocator()->get('Users\Model\OrdersInfoFactory');
		$orderstatusTable=$this->getServiceLocator()->get('Users\Model\OrdersStatusFactory');
		$addOrderId=$orderstatusTable->addOrderstatus();
		if($addOrderId>0){
			for($i=0;$i<count($_SESSION['orders']);$i++){
				if(!isset($_SESSION['orders'][$i])){ continue; }
				$addOrderMain_id=$ordermainTable->addOrder($addOrderId,$_SESSION['orders'][$i]['Main_array']);
				if($addOrderMain_id>0){
					$main_id=$addOrderMain_id;
					if(isset($_SESSION['orders'][$i]['Print_Paper'])
					&& !isset($_SESSION['orders'][$i]['Magazine'])
					&& !isset($_SESSION['orders'][$i]['Home_delivery'])
					){ 
						$_SESSION['orders'][$i]['Print_Paper']=$_SESSION['orders'][$i]['Print_Paper'];
						$_SESSION['orders'][$i]['Magazine']='';
						$_SESSION['orders'][$i]['Home_delivery']='';						
					}else
					if(isset($_SESSION['orders'][$i]['Print_Paper'])
					&& isset($_SESSION['orders'][$i]['Magazine']) 
					&& !isset($_SESSION['orders'][$i]['Home_delivery']) )
					{
						$_SESSION['orders'][$i]['Print_Paper']=$_SESSION['orders'][$i]['Print_Paper'];
						$_SESSION['orders'][$i]['Magazine']=$_SESSION['orders'][$i]['Magazine'];
						$_SESSION['orders'][$i]['Home_delivery']='';
					}else					
					if(!isset($_SESSION['orders'][$i]['Print_Paper']) 
					&& isset($_SESSION['orders'][$i]['Magazine']) 
					&& !isset($_SESSION['orders'][$i]['Home_delivery']))
					{
						$_SESSION['orders'][$i]['Print_Paper']='';
						$_SESSION['orders'][$i]['Magazine']=$_SESSION['orders'][$i]['Magazine'];
						$_SESSION['orders'][$i]['Home_delivery']='';
					}else
					if(!isset($_SESSION['orders'][$i]['Print_Paper'])
					&& isset($_SESSION['orders'][$i]['Magazine'])
					&& isset($_SESSION['orders'][$i]['Home_delivery']))
					{
						$_SESSION['orders'][$i]['Print_Paper']='';
						$_SESSION['orders'][$i]['Magazine']=$_SESSION['orders'][$i]['Magazine'];
						$_SESSION['orders'][$i]['Home_delivery']=$_SESSION['orders'][$i]['Home_delivery'];
					}else
					if(!isset($_SESSION['orders'][$i]['Print_Paper'])
					&& !isset($_SESSION['orders'][$i]['Magazine'])
					&& isset($_SESSION['orders'][$i]['Home_delivery']))
					{
						$_SESSION['orders'][$i]['Print_Paper']='';
						$_SESSION['orders'][$i]['Magazine']='';
						$_SESSION['orders'][$i]['Home_delivery']=$_SESSION['orders'][$i]['Home_delivery'];
					}else
					if(isset($_SESSION['orders'][$i]['Print_Paper'])
					&& !isset($_SESSION['orders'][$i]['Magazine'])
					&& isset($_SESSION['orders'][$i]['Home_delivery']))
					{
						$_SESSION['orders'][$i]['Print_Paper']=$_SESSION['orders'][$i]['Print_Paper'];
						$_SESSION['orders'][$i]['Magazine']='';
						$_SESSION['orders'][$i]['Home_delivery']=$_SESSION['orders'][$i]['Home_delivery'];
					}else
					if(isset($_SESSION['orders'][$i]['Print_Paper'])
					&& isset($_SESSION['orders'][$i]['Magazine']) 
					&& isset($_SESSION['orders'][$i]['Home_delivery']) )
					{
						$_SESSION['orders'][$i]['Print_Paper']=$_SESSION['orders'][$i]['Print_Paper'];
						$_SESSION['orders'][$i]['Magazine']=$_SESSION['orders'][$i]['Magazine'];
						$_SESSION['orders'][$i]['Home_delivery']=$_SESSION['orders'][$i]['Home_delivery'];
					}
					$addOrderMain=$orderinfoTable->addOrderinfo($main_id,$_SESSION['orders'][$i]['Print_Paper'],$_SESSION['orders'][$i]['Magazine'],$_SESSION['orders'][$i]['Home_delivery']);
					if($addOrderMain>0){
						if($_SESSION['orders']['ad_mode']=='offline'){
						  return $this->redirect()->toUrl($baseUrl.'admin/payment-success?oId='.$addOrderId);
						}else if($_SESSION['orders']['ad_mode']=='online'){
						  return $this->redirect()->toUrl($baseUrl.'admin/payments?order_id='.$addOrderId);
						}						 
					}				
				}
			}
		}
	}
	public function paymentSuccessAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$token=$this->mt_rand_str('16');
		global $confirmOrderSubject;
	    global $confirmOrderMessage;
		$orderstatusTable=$this->getServiceLocator()->get('Users\Model\OrdersStatusFactory');
		if(isset($_GET['tx']) && $_GET['tx']!=''){
			$getoderDetail=$orderstatusTable->getOrderInfo_d($_GET['item_number']);
			if(count($getoderDetail)>='0'){
				if($_SESSION['user']['user_id']!=$getoderDetail->user_id){
					$userName=$getoderDetail->user_name;
					$to=$getoderDetail->email;
				}else{
					$userName=$getoderDetail->guest_email;
					$to=$getoderDetail->guest_email;
				}
				$orderId=$getoderDetail->ads_order_id;
				$orderAmount=$getoderDetail->tot_amount;
				$orderFrom=$getoderDetail->ads_mode;
				$paymentStatus=$getoderDetail->payment_status;				
				$body=$confirmOrderMessage;
				$body = str_replace("<FULLNAME>",$userName, $body);
				$body = str_replace("<ORDERID>",$orderId, $body);
				$body = str_replace("<ORDERFROM>",$orderFrom, $body);
				$body = str_replace("<USERNAME>",$userName, $body);
				$body = str_replace("<ORDERTOTAL>",$orderAmount, $body);
				$body = str_replace("<PAYMENTSTATUS>",$paymentStatus, $body);
			}
			$sentMail=sendMail($to,$confirmOrderSubject,$body);	
			if($sentMail>0){
				return $this->redirect()->toUrl($baseUrl.'admin/order-success');
			}else{
				return $this->redirect()->toUrl($baseUrl.'admin/order-success');
			}
		}else if(isset($_GET['oId']) && $_GET['oId']!=''){
			$oderId=$_GET['oId'];
			$tax_id=$token;
			$status='Pending';			
			$updateOrderStatus=$orderstatusTable->orderSuccess($oderId,$tax_id,$status);
			if($updateOrderStatus>=0){
				$getoderDetail=$orderstatusTable->getOrderInfo_d($oderId);
				if(count($getoderDetail)>='0'){
					if($_SESSION['admin']['user_id']!=$getoderDetail->user_id){
						$userName=$getoderDetail->user_name;
						$to=$getoderDetail->email;
					}else{
						$userName=$getoderDetail->guest_email;
						$to=$getoderDetail->guest_email;
					}
					$orderId=$getoderDetail->ads_order_id;
					$orderAmount=$getoderDetail->tot_amount;
					$orderFrom=$getoderDetail->ads_mode;
					$paymentStatus=$getoderDetail->payment_status;				
					$body=$confirmOrderMessage;
					$body = str_replace("<FULLNAME>",$userName, $body);
					$body = str_replace("<ORDERID>",$orderId, $body);
					$body = str_replace("<ORDERFROM>",$orderFrom, $body);
					$body = str_replace("<USERNAME>",$userName, $body);
					$body = str_replace("<ORDERTOTAL>",$orderAmount, $body);
					$body = str_replace("<PAYMENTSTATUS>",$paymentStatus, $body);
				}
				$sentMail=sendMail($to,$confirmOrderSubject,$body);	
				if($sentMail>0){
					return $this->redirect()->toUrl($baseUrl.'admin/order-success');
				}else{
					return $this->redirect()->toUrl($baseUrl.'admin/order-success');
				}					
			}	
		}else{
			return $this->redirect()->toUrl($baseUrl.'admin/order-success');
		}		
	}
	public function orderSuccessAction(){
		unset($_SESSION['orders']);
	}
	public function paymentsAction(){
		
	}
	public function totalonlineordersAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');		
		$orderstatusTable=$this->getServiceLocator()->get('Users\Model\OrdersStatusFactory');
		if(isset($_GET['admode']) && $_GET['admode']!=""){
			$adMode=$_GET['admode'];
		}
		if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			$userId=$_GET['user_id'];
			$getUserDetails=$usersTable->getuserDetailes($userId);
			$distId=$getUserDetails->district_id;
			$areaId=$getUserDetails->area_id;
			$locId=$getUserDetails->location_id;
		}else{
			$userId='';
			$distId='';
			$areaId='';
			$locId='';
		}
		if(isset($_GET['cat_id']) && $_GET['cat_id']!=""){
			$catId=$_GET['cat_id'];
		}else{
			$catId='';
		}
		if(isset($_GET['sub_cat_id']) && $_GET['sub_cat_id']!=""){
			$subcatId=$_GET['sub_cat_id'];
		}else{
			$subcatId='';
		}
		$searchkey='';
		$result=array();
		$sOutput='';	
		if($adMode=='online'){
			$aColumns = array('ads_order_id','user_name','phone_number','tot_amount','order_status','ads_order_id','ads_order_id','status');
		}else if($adMode=='offline'){
			$aColumns = array('ads_order_id','user_name','phone_number','tot_amount','amount_paid','order_status','ads_order_id','status');
		}		
		$total = $orderstatusTable->getcount($_POST);
		$paginator = $orderstatusTable->getonlineorderslistsGrid($searchkey,$aColumns,$_POST,$userId,$distId,$areaId,$locId,$catId,$subcatId,$adMode,true);
		$result['rResult']=array();
		foreach($paginator as $results){
			if(count($results)!=0){
				$result['rResult'][]=$results;
			}
		}
		if(count($result)!=0){
			$result['iTotal']=$total;
			$result['iFilteredTotal']=$total;
			$result['sEcho']=intval($_POST['sEcho']);
			$sOutput = '{';
			$sOutput .= '"sEcho": '.$result['sEcho'].', ';
			$sOutput .= '"iTotalRecords": '.$result['iTotal'].', ';
			$sOutput .= '"iTotalDisplayRecords": '.$result['iFilteredTotal'].', ';
			$sOutput .= '"aaData": [ ';
			for($m=0;$m<count($result['rResult']);$m++)
			{
				$sOutput .= "[";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					if($adMode=='online'){
						$output_item = $result['rResult'][$m]->$aColumns[$i];
						if($i=='0'){
							$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->ads_order_id).'" />';
						}
						if($i=='1'){
							if($result['rResult'][$m]->user_type_id=1 || $result['rResult'][$m]->user_type_id=2){
								$output_item = $result['rResult'][$m]->guest_email;
							}else{
								$output_item = $result['rResult'][$m]->user_name;
							}
						}
						if($i=='2'){
							if($result['rResult'][$m]->user_type_id=1 || $result['rResult'][$m]->user_type_id=2){
							    $output_item = $result['rResult'][$m]->guest_phone_number;
							}else{
								$output_item = $result['rResult'][$m]->phone_number;
							}
						}
						if($i=='3'){
							$output_item = $result['rResult'][$m]->tot_amount.' Rs';
						}
						if($i=='4'){
							$output_item = $result['rResult'][$m]->order_status;
						}
						if($result['rResult'][$m]->status == 0){
							if($i=='5'){
								$output_item ='Change Order Status';
							}
						}else{
							if($i=='5'){
								$output_item ='<a href='.$baseUrl.'admin/order-change?order_id='.$result['rResult'][$m]->ads_order_id.'  title="Change Status" class="">Change Status</a>';
							}
						}
						if($result['rResult'][$m]->status == 0){
							if($i=='6'){
								$output_item ='Edit';
							}
						}else{
							if($i=='6'){
								$output_item ='<a href='.$baseUrl.'admin/edit-ad-posting?order_id='.$result['rResult'][$m]->ads_order_id.'  title="Edit" class="">Edit</a>';
							}
						}
						if($aColumns[$i]=='status'){ 
							if($result['rResult'][$m]->status == 1) $output_item = 'Active';
							else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
						}
						$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
					}else if($adMode=='offline'){
						$output_item = $result['rResult'][$m]->$aColumns[$i];
						if($i=='0'){
							$output_item = '<input type="checkbox" name="check[]" id="check" value="'.base64_encode($result['rResult'][$m]->ads_order_id).'" />';
						}
						if($i=='1'){
							if($result['rResult'][$m]->user_type_id=1 || $result['rResult'][$m]->user_type_id=2){
								$output_item = $result['rResult'][$m]->guest_email;
							}else{
								$output_item = $result['rResult'][$m]->user_name;
							}
						}
						if($i=='2'){
							if($result['rResult'][$m]->user_type_id=1 || $result['rResult'][$m]->user_type_id=2){
							    $output_item = $result['rResult'][$m]->guest_phone_number;
							}else{
								$output_item = $result['rResult'][$m]->phone_number;
							}
						}
						if($i=='3'){
							$output_item = $result['rResult'][$m]->tot_amount.' Rs';
						}
						if($i=='4'){
							if($result['rResult'][$m]->amount_paid=='0'){
								$output_item = 'Paid';
							}else if($result['rResult'][$m]->amount_paid==''){
								$output_item = $result['rResult'][$m]->tot_amount.' Rs';
							}else{
								$output_item = $result['rResult'][$m]->amount_paid.' Rs';
							}
						}
						if($i=='5'){
							$output_item = $result['rResult'][$m]->order_status;
						}
						if($result['rResult'][$m]->status == 0){
							if($i=='6'){
								$output_item ='Edit';
							}
						}else{
							if($i=='6'){
								$output_item ='<a href='.$baseUrl.'admin/edit-ad-posting?order_id='.$result['rResult'][$m]->ads_order_id.'  title="Edit" class="">Edit</a>';
							}
						}
						if($aColumns[$i]=='status'){ 
							if($result['rResult'][$m]->status == 1) $output_item = 'Active';
							else if($result['rResult'][$m]->status == 0) $output_item = 'Deactive';
						}
						$sOutput .= '"'.str_replace('"', '\"', $output_item).'",';
					}
				}
				$sOutput = substr_replace( $sOutput, "", -1 );
				$sOutput .= "],";
			}
			$sOutput = substr_replace( $sOutput, "", -1 );
			$sOutput .= '] }';
		}else{
			$sOutput = '{';
			$sOutput .= '"aaData": [ ';
			$sOutput .= '"sEcho": '.$_POST['sEcho'].', ';
			$sOutput .= '"iTotalRecords": 0, ';
			$sOutput .= '"iTotalDisplayRecords": 0, ';
			$sOutput .= '] }';
		}
		echo $sOutput;exit;		
	}
	public function onlineOrdersAction(){
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
		$subcategoryTable=$this->getServiceLocator()->get('Category\Model\SubCategoryFactory');
		$getCategories=$categoryTable->getallCategories();
		$getAllFranchisee=$usersTable->getallFranchisee();
		$result = new ViewModel(array(
			'all_categories'          =>$getCategories,				
			'all_franchisee'          =>$getAllFranchisee,				
		));	
		return $result;
	}
	public function offlineOrdersAction(){
		$usersTable=$this->getServiceLocator()->get('Users\Model\UsersFactory');
		$categoryTable=$this->getServiceLocator()->get('Category\Model\CategoryFactory');
		$subcategoryTable=$this->getServiceLocator()->get('Category\Model\SubCategoryFactory');
		$getCategories=$categoryTable->getallCategories();
		$getAllFranchisee=$usersTable->getallFranchisee();
		$result = new ViewModel(array(
			'all_categories'          =>$getCategories,				
			'all_franchisee'          =>$getAllFranchisee,				
		));	
		return $result;
	}
	public function mt_rand_str ($l, $c = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
		for ($s = '', $cl = strlen($c)-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
		return $s;
	}
	public function orderChangeAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$orderstatusTable=$this->getServiceLocator()->get('Users\Model\OrdersStatusFactory');
		if(isset($_POST['order_id']) && $_POST['order_id']!=""){
			if($updateOrderstatus>=0){
				$ordermainTable=$this->getServiceLocator()->get('Users\Model\OrderMainsFactory');
				return $this->redirect()->toUrl($baseUrl.'admin/online-orders');	
			}
		}else if(isset($_GET['order_id']) && $_GET['order_id']!=""){			
			$getOrderInfo=$orderstatusTable->getOrderinfo($_GET['order_id']);
			$result = new ViewModel(array(
				'getOrderInfo'          =>$getOrderInfo->current(),				
			));	
			return $result;
		}
	}
	public function newsletterAction(){
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$newsletterTable=$this->getServiceLocator()->get('Users\Model\NewsletterFactory');
		if(isset($_POST['message']) && $_POST['message']!=""){
			$newsletterAdd=$newsletterTable->addNewsLetterToUsers($_POST['subject'],$_POST['message']);					
		}
	}
	public function sendnewsletterAction(){
		$subscribeTable=$this->getServiceLocator()->get('Users\Model\SubscribeFactory');
		$newsletterTable=$this->getServiceLocator()->get('Users\Model\NewsletterFactory');
		$userNewsletterTable=$this->getServiceLocator()->get('Users\Model\UsernewsletterFactory');
		$subscribeAll=$subscribeTable->allSubScribers();
		if($subscribeAll->count()>0){ 			
				foreach($subscribeAll as $subscribedusers){
					$subscribe_id=$subscribedusers->subscribe_id;
						$checkNewsletter=$userNewsletterTable->checkNewsLetterSend($subscribe_id,'2');
						if($checkNewsletter==0){
							$userNewsletter=$userNewsletterTable->addUsersNewsletter($subscribe_id,'2');
							echo "Insert Sucessfully email_id is ".$subscribe_id.'<br/>';
						}else {
							echo "Already exist ".$subscribe_id.'<br/>';
						}
				}
		}
		echo "sucess";exit;
		
	}
	
	
}
	